<?php

class Mycenter_model extends CI_Model{
	const tableName = "user";
	
	function __construct(){
		parent::__construct();
	}
	
	function readOrderList($whereTime, $limit, $whereUserid)
	{
		$orderList=array();		
		$sql = " select orderdetail.id orderdetailId, serialNumber,restaurant.name restaurantName,restaurant.phone restaurantPhone,
		        orderdetail.totalPrice,left(ctime,16) orderCreateTime,orderdetail.status orderdetailStatus  
				from orderdetail, restaurant    
				where orderdetail.restaurantId=restaurant.id".$whereTime.$whereUserid.
				" ORDER BY orderdetail.ctime desc;";
		
		// 除了食物之外的项
		$oLexFood = $this->db->query($sql) or die("error");
		$i = 0;
		foreach ($oLexFood->result_array() as $row){
			$orderList[ $i ]['serialNumber'] = $row[ 'serialNumber'] ; // 订单号
			$orderList[ $i ]['restaurantName'] = $row[ 'restaurantName']; // 餐馆名称
			$orderList[ $i ]['restaurantPhone'] = $row['restaurantPhone']; 
			$orderList[ $i ]['totalPrice'] = $row['totalPrice']; 
			$orderList[ $i ]['orderCreateTime'] = $row['orderCreateTime']; 
			$orderList[ $i ]['orderdetailStatus'] = $row['orderdetailStatus']; 			
			// 查询food项
			$sqlF = "select 	food.name, food.unitPrice, orderitem.quantity  
					 from 		orderdetail, orderitem, food  
					 where 		orderdetail.id=".$row['orderdetailId']." and orderdetail.id=orderitem.orderId and orderitem.foodId=food.id;";
			
			$oLFood = $this->db->query($sqlF) or die("error"); 
			$orderList[ $i ]['orderFoodList'] = $oLFood->result_array();
			$oLFood->free_result();
			$i++;
		}
		$oLexFood->free_result();
		return $orderList; // 返回适合网页展示的订单列表
	}
	
	// 修改密码
	public function changePassword($data)
	{
		
		$update = array('password' => md5($data['newPasswd']));
		$this->db->where('id', $data['userId']);
		$this->db->update(self::tableName, $update);
		
		$query = $this->db->get_where(self::tableName, array('id' => $data['userId'],'password' => md5($data['newPasswd'])));
		if ($row = $query->row_array())
		{
			return TRUE;
		}else{
			return FALSE;
		}
				
	}
	
}
?>